﻿using BlueportCoffee.http;
using BlueportCoffee.MilkTeaModel;
using BlueportCoffee.model;
using BlueportCoffee.SocketModel;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;

namespace BlueportCoffee.IceModel
{
    public class IceMakingMsg
    {
        private static IceMakingMsg instance;
        private IceMakingMsg() { }

        public static IceMakingMsg Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new IceMakingMsg();
                }
                return instance;
            }
        }
        #region IO通讯
        public bool AllBol = true;
        public bool RobotBol = true;
        public delegate void IOdelegate(string btnName, string tag);
        public IOdelegate iodelegate;
        /// <summary>
        ///  IO的通讯返回数据
        /// </summary>
        private List<byte> buffer = new List<byte>();
        public static bool IsMsgError = true;
        string nostrs = "";
        public Thread All_thr;
        public int IceInit = 0;
        public void IOSerial_Thr()
        {
            try
            {
                var ddd = SerialPort.GetPortNames().ToList();
                if (!SerialPort.GetPortNames().ToList().Any(a => a.ToLower().Contains(IPorPortInfo.Instance.ZBPortName)))
                {
                    Thread.Sleep(1000);
                    IOSerial_Thr();
                }
                IceMakingSerialPort.Instance.IceMachineConnect(IPorPortInfo.Instance.ZBPortName, 9600, "", 8, 1);//其他串口通讯
                IceMakingSerialPort.Instance.serialPort.DataReceived += SerialPort_DataReceived;
                Alter alter = new Alter();
                alter.equipNo = "10020";
                alter.equipAlterInfo = "制冰机冰量未达出冰标准";
                alter.equipAlterType = "2";
                string lists = JsonConvert.SerializeObject(alter);
                HttpMsg.Instance.saveDeviceMatterMsg(lists);
                Thread All_thr = new Thread(AllMassage);
                All_thr.IsBackground = true;
                All_thr.Start();
            }
            catch (Exception)
            {
                IceMakingSerialPort.Instance.serialPort.DataReceived -= SerialPort_DataReceived;
                Thread.Sleep(1000);
                IceInit++;
                if (IceInit == 20)
                {
                    //EmailShow emailShow = new EmailShow();
                    //emailShow.content = EmailError();
                    //var saves1 = JsonConvert.SerializeObject(emailShow);
                    //var dd1 = HttpMsg.Instance.sendEmailMsg(saves1);
                }
                IOSerial_Thr();
            }
        }
        public void AllMassage()
        {
            try
            {
                while (true)
                {
                    try
                    {
                        if (Instance.AllBol)
                        {
                            IceMakingSerialPort.Instance.SerialSend(IceMakingModel.Instance.AllMessage);
                            // Console.WriteLine("zhibing");
                        }
                        Thread.Sleep(300);
                    }
                    catch (Exception)
                    {
                        Thread.Sleep(300);
                    }

                }
            }
            catch (Exception)
            {
            }
        }
        private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                byte[] result = new byte[IceMakingSerialPort.Instance.serialPort.BytesToRead];
                IceMakingSerialPort.Instance.serialPort.Read(result, 0, IceMakingSerialPort.Instance.serialPort.BytesToRead);
                string str = "";// $"{DateTime.Now}：\n";
                str += $"{Encoding.ASCII.GetString(result)}";
                for (int i = 0; i < result.Length; i++)
                {
                    str += $"{result[i].ToString("X2")} ";
                }
                Console.WriteLine(str);

                MsgShow(str);
                Thread.Sleep(1);
            }
            catch (Exception)
            {
                return;
            }
        }
        string zblist = "";
        public bool ErrorBol = false;
        public bool ZBJBol = false;
        public bool ZBJWCBol = false;

        public void MsgShow(string str)
        {
            try
            {
                var Ice = str.Split(' ');
                if (Ice.Length >= 4)
                {
                    if (str.Contains("AA 01 02 55") || str.Contains("AA 01 08 55"))
                    {
                        if (!ErrorBol)
                        {
                            Instance.ZBJBol = true;
                            Instance.ErrorBol = true;
                            Alter alter = new Alter();
                            alter.equipNo = "10020";
                            alter.equipAlterInfo = "制冰机可出冰";
                            alter.equipAlterType = "4";
                            string lists = JsonConvert.SerializeObject(alter);
                            HttpMsg.Instance.saveDeviceMatterMsg(lists);
                            WuLiaoCoffeeSel.Instance.Ice1_Sel_Bol = true;
                            ClientList clientList = new ClientList();
                            clientList.MsgBol = WuLiaoCoffeeSel.Instance;
                            clientList.code = 2;
                            SocketClients.Instance.client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList)));
                        }
                    }
                    else if (str.Contains("AA 05 20 55") || str.Contains("AA 05 40 55") || str.Contains("AA 05 60 55"))
                    {
                        ZBJWCBol = true;
                        Console.WriteLine(str + "  " + DateTime.Now.ToString());
                    }
                    else
                    {
                        if (ErrorBol)
                        {
                            Instance.ZBJBol = false;
                            Instance.ErrorBol = false;
                            Alter alter = new Alter();
                            alter.equipNo = "10020";
                            alter.equipAlterInfo = "制冰机冰量未达出冰标准";
                            alter.equipAlterType = "2";
                            string lists = JsonConvert.SerializeObject(alter);
                            HttpMsg.Instance.saveDeviceMatterMsg(lists);
                            WuLiaoCoffeeSel.Instance.Ice1_Sel_Bol = false;
                            ClientList clientList = new ClientList();
                            clientList.MsgBol = WuLiaoCoffeeSel.Instance;
                            clientList.code = 2;
                            SocketClients.Instance.client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList)));
                        }
                    }
                }
                //var Ice = str.Split(' ');
                //if (Ice.Length >= 4)
                //{

                //    if (Ice[1] == "01")
                //    {
                //        if (Ice[2] == "02" || Ice[2] == "08")
                //        {
                //            Instance.ZBJBol = true;
                //            Instance.ErrorBol = true;
                //        }
                //        else
                //        {
                //            if (ErrorBol)
                //            {
                //                Instance.ZBJBol = false;
                //                Instance.ErrorBol = false;
                //                Alter alter = new Alter();
                //                alter.equipNo = "10020";
                //                alter.equipAlterInfo = "制冰机冰量未达出冰标准";
                //                alter.equipAlterType = "2";
                //                string lists = JsonConvert.SerializeObject(alter);
                //                HttpMsg.Instance.saveDeviceMatterMsg(lists);
                //            }
                //        }
                //    }
                //    else if (Ice[1] == "05")
                //    {
                //        ZBJWCBol = true;
                //        Console.WriteLine(str+"  "+DateTime.Now.ToString());
                //    }
                //}
            }
            catch (Exception)
            {
                return;
            }
        }
        #endregion
    }
}
